#ifndef __M_UTILS_H__
#define __M_UTILS_H__

#include <stdint.h>

#include <psp2/rtc.h>
#include <psp2/ctrl.h>
#include <psp2/gxm.h>
#include <psp2/touch.h>

#include "ctrl_pad.h"
#include "touch_pad.h"

#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

#define MAX_CTRL_COUNT_NUM 65535

enum ScrollTypes
{
    SCROLL_NONE = 0x00000000,
    SCROLL_UP = 0x00000001,
    SCROLL_DOWN = 0x00000002,
    SCROLL_LEFT = 0x00000004,
    SCROLL_RIGHT = 0x00000008,
};

enum ControlTypes
{
    MOVE_TYPE_NONE,
    MOVE_TYPE_PREV,
    MOVE_TYPE_NEXT,
    MOVE_TYPE_PREV_PAGE,
    MOVE_TYPE_NEXT_PAGE,
};

extern CtrlPad ctrl_pad;
extern TouchPad touch_pad;

void resetDebugScreenXY();
void debugScreenPrintf(char *text, ...);
int APP_LOG(char *text, ...);

void readPad(CtrlPad *pad);
void resetPad(CtrlPad *pad);
void readTouch(TouchPad *pad);
void resetTouch(TouchPad *pad);

int isValidLocation(int x, int y, int sx, int sy, int dx, int dy);

int hasEndSlash(const char *path);
int removeEndSlash(char *path);
int addEndSlash(char *path);

void convertUtcToLocalTime(SceDateTime *time_local, SceDateTime *time_utc);
void convertLocalTimeToUtc(SceDateTime *time_utc, SceDateTime *time_local);

void getSizeString(char string[16], uint64_t size);
void getDateString(char string[24], int date_format, SceDateTime *time);
void getTimeString(char string[16], int time_format, SceDateTime *time);
void getDurationString(char string[16], uint64_t time);

void refreshListPos(int *top_pos, int *focus_pos, int length, int lines);
void moveRefreshListPos(int type, int *top_pos, int *focus_pos, int length, int lines);

void refreshListPosNoFocus(int *top_pos, int length, int lines);
void moveRefreshListPosNoFocus(int type, int *top_pos, int length, int lines);

void lockHome();
void unlockHome();
void lockUsbConnection();
void unlockUsbConnection();
void lockQuickMenu();
void unlockQuickMenu();
void lockSuspend();
void unlockSuspend();
void lockOledOff();
void unlockOledOff();
void lockOledDimming();
void unlockOledDimming();

void initPowerTickThread();

#endif
